package servlet;


import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/")
public class Servlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        StringBuilder html = new StringBuilder("<!DOCTYPE html> \n");

        html.append("<html>\n");
        html.append("<head><meta charset=\"utf-8\"></head>\n");
        html.append("<body bgcolor=\"#f0f0f0\">\n");
        html.append("<h1 align=\"center\">HTTP Header Table</h1>\n");

        html.append("<table width=\"100%\" border=\"1\" align=\"center\">\n");
        html.append("<tr bgcolor=\"#949494\">\n<th>Header Param</th><th>Header Value</th>\n</tr>\n");


        Enumeration headerNames = request.getHeaderNames();

        while (headerNames.hasMoreElements()) {
            String paramName = (String) headerNames.nextElement();
            html.append("<tr><td>" + paramName + "</td>\n");
            html.append("<td> " + request.getHeader(paramName) + "</td></tr>\n");
        }
        html.append("</table>\n</body>\n</html>");

        response.getWriter().println(html);
    }
}